package com.teasale.controller;


import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.teasale.vo.OrderGoodsOV;
import com.teasale.entity.SUser;
import com.teasale.service.SUserService;

import com.teasale.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * (SUser)表控制层
 *
 * @author makejava
 * @since 2023-06-06 00:54:05
 */
@RestController
public class SUserController {
    /**
     * 服务对象
     */
    @Resource
    private SUserService sUserService;



    /**
     * 登录
     * @param user
     * @param session
     * @return
     */
    @PostMapping(value = "api/login")
    @ResponseBody
    public Result login(@RequestBody SUser user, HttpSession session){
        System.out.println(user.getUserName());
        String userName = user.getUserName();
        String userPassword = user.getUserPassword();
        SUser login = sUserService.login(userName, userPassword);
        if (null == login) {
            return new Result(400);
        } else {
            session.setAttribute("user", login);
            return new Result(200);
        }
    }

    /**
     * 获取用户信息
     * @param username
     * @return
     */
    @GetMapping("api/getOneUser")
    public SUser findOne(@RequestParam String username){
        return sUserService.findOnde(username);
    }

    /**
     根据用户名获取订单列表
     **/
    @GetMapping("api/orders/getOneByName")
    public List<OrderGoodsOV> getAll(@RequestParam String username){
        List<OrderGoodsOV> orderGoodsOVS = sUserService.findAllOrder(username);
        return orderGoodsOVS;
    }


    /**
     删除用户
     **/
    @PostMapping("api/suser/delUser")
    public String delUser(@RequestBody Map<String, Integer> param){
        Integer userId = param.get("userId");
        sUserService.delUser(userId);
        return "ok";
    }
    /**
     注册
     **/
    @PostMapping("api/suser/register")
    public SUser register(@RequestBody SUser sUser){

        return sUserService.addUser(sUser);
    }

    /**
     * 获取所有用户信息
     * @param
     */
    @GetMapping("api/getAllUser")
    public List<SUser> findAll(){
        return sUserService.query().list();
    }


}

